package m202406.d09;

import java.util.List;
import java.util.PriorityQueue;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/6/9 13:17
 */
public class Q23 {
    public ListNode mergeKLists(ListNode[] lists) {
        PriorityQueue<ListNode> queue = new PriorityQueue<>((o1, o2) -> o1.val - o2.val);
        for (ListNode list : lists) {
            if (list == null) {
                continue;
            }
            queue.offer(list);
        }
        ListNode res = new ListNode();
        ListNode p = res;
        while (!queue.isEmpty()) {
            ListNode poll = queue.poll();
            p.next = new ListNode(poll.val);
            p = p.next;
            poll = poll.next;
            if (poll == null) {
                continue;
            }
            queue.offer(poll);
        }
        return res.next;
    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
